layui.define(['layer', 'laypage'], function(e) {
    "use strict";
    var $ = layui.$, laypage = layui.laypage, layer = layui.layer;

    var opts = {
        type_id: '',    // 类别（1 活动 2公告，不填就是全部）
        curr:1,         // 页码
        limit:10,       // 页显示数量
		total: 1		// 总数量
    };

    function getList(opts){
        layer.load(2, {shade: [0.7, '#fff'], time: 5000});
        $.ajax({
            url: '/notice/getList',
            type: 'POST',
            dataType: 'json',
            data: {page: opts.cur,limit:opts.limit,type_id:opts.type_id}
        })
        .done(function(res) {
            var html = "";
            if (res.code == 0) {
                var data = res.data;
                opts.total = parseInt(data.total);
                opts.cur = parseInt(data.cur_page);
				var lists = data.list;
				if(lists.length>0){
					for (var i = 0,j = lists.length; i < j; i++) {

						html +=  '<a class="news-item" href="/notice/'+ lists[i].id +'.html">'
								+    '<div class="news-item-hd layui-clear">'
								+        '<h2 class="news-item-title">'+ lists[i].title +'</h2>'
								+        '<p class="news-pubtime">'+ lists[i].publishTime +'</p>'
								+    '</div>'
								+    '<div class="news-item-bd">'
								+        '<p class="news-item-txt">'+ lists[i].description +'</p>'
								+    '</div>'
								+'</a>';

					}
					//设置分页
					if(opts.total > opts.limit){
						laypage.render({
							elem: 'J_pages',
							curr: opts.cur,
							count: opts.total,
							theme: '#0045ad',
							groups: 3,
							limit: opts.limit,
							prev: '<i class="layui-icon">&#xe603;</i>',
							next: '<i class="layui-icon">&#xe602;</i>',
							layout: ['prev', 'page', 'next', 'count', 'skip'],
							jump: function(obj, first){
								if(!first){
									opts.cur = obj.curr;
									getList(opts);
									scrollTo(0,document.getElementById('J_result').offsetTop+30);
								}
							}
						});
						$('#J_pages').css({
							'text-align':'right',
							'border-top':'1px solid #f2f2f2',
							'padding-top':'20px'
						})
					}
				} else {
					opts.curr=1;
					html = '<div class="empty-all"><img class="empty-img" src="/res/v1/images/empty/4.png"><p class="empty-txt">平台暂未发布动态</p></div>';
				}
				$('#J_result').html(html);
			} else {
                layer.msg(res.msg, {icon:5})
            }
        })
        .always(function() {
            layer.closeAll('loading');
        });
    }
    getList(opts);
	// 筛选
	$('.tag-type-item').each(function(idx, elem){
		$(elem).on('click', function(){
			opts.type_id = $(this).data('type')||'';
			$(this).addClass('active').siblings().removeClass('active');
			getList(opts);
		})
	})
    e('notice', {});
});
